<?php

include_once('Database.php');

class Vehicle 
{
	private $vehicleId;
	private $currentTime;
	private $status;
	private $position;	
	private $battery;
	private $distanceTravelled;
	private $destination;
	private $freeSeats;
	private $speed;

	public function __construct($vehicleId, $currentTime, $status, $position, $battery, $distanceTravelled, $destination, $freeSeats, $speed)
	{
		$this->setVehicleId($vehicleId);
		$this->setCurrentTime($currentTime);
		$this->setStatus($status);
		$this->setPosition($position);
		$this->setBattery($battery);
		$this->setDistanceTravelled($distanceTravelled);
		$this->setDestination($destination);
		$this->setFreeSeats($freeSeats);
		$this->setSpeed($speed);
	}

	public function getVehicleId()
	{
		return $this->vehicleId;
	}

	public function setVehicleId($vehicleId)
	{
		$this->vehicleId = $vehicleId;
	}

	public function getCurrentTime()
	{
		return $this->currentTime;
	}

	public function setCurrentTime($currentTime)
	{
		$this->currentTime = $currentTime;
	}

	public function getStatus()
	{
		return $this->status;
	}

	public function setStatus($status)
	{
		$this->status = $status;
	}

	public function getPosition()
	{
		return $this->position;
	}

	public function setPosition($position)
	{
		$this->position = $position;
	}

	public function getBattery()
	{
		return $this->battery;
	}

	public function setBattery($battery)
	{
		$this->battery = $battery;
	}

	public function getDistanceTravelled()
	{
		return $this->distanceTravelled;
	}

	public function setDistanceTravelled($distanceTravelled)
	{
		$this->distanceTravelled = $distanceTravelled;
	}

	public function getDestination()
	{
		return $this->destination;
	}

	public function setDestination($destination)
	{
		$this->destination = $destination;
	}

	public function getFreeSeats()
	{
		return $this->freeSeats;
	}

	public function setFreeSeats($freeSeats)
	{
		$this->freeSeats = $freeSeats;
	}

	public function getSpeed()
	{
		return $this->speed;
	}

	public function setSpeed($speed)
	{
		$this->speed = $speed;
	}
     
	 	public function jsonSerialize()
	{
		$row_array = array();
		$row_array['vehicleId'] = $this->getVehicleId();
		$row_array['status'] = $this->getStatus();
		$row_array['position'] = $this->getPosition();
		$row_array['battery'] = $this->getBattery();
		$row_array['distanceTravelled'] = $this->getDistanceTravelled();
		$row_array['destination'] = $this->getDestination();
		$row_array['freeSeats'] = $this->getFreeSeats();
		$row_array['speed'] = $this->getSpeed();
		return $row_array;
	}
}

$path = $_SERVER['PATH_INFO']; 

if($path != null)
{
    $path_params = explode ("/", $path);
}

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        
        if ($path_params[1] != null) {
        get_vehicle($path_params[1]);
        } 
        else {
                get_vehicles();
        }
}

function get_vehicle($vehicleId) {
		$db = new DBManager();
        $mysqli = $db->db_connect();

		$query = "SELECT v.vehicleId, v.currentTime, v.status, v.position, v.battery, v.distanceTravelled, v.destination, v.freeSeats, v.speed 
		FROM VehicleInfo as v WHERE v.vehicleId = ?";
		
		$stmt=$mysqli->prepare($query);
	    $stmt->bind_param('i',$vehicleId);
        $stmt->execute();	
        $result = $stmt->get_result();	
		
	    print_result($result);
}

function get_vehicles() {
      
   	    $db = new DBManager();
        $mysqli = $db->db_connect();
		
		$query = "SELECT v.vehicleId, v.currentTime, v.status, v.position, v.battery, v.distanceTravelled, v.destination, v.freeSeats, v.speed 
		FROM VehicleInfo as v";
    	
        
		$stmt=$mysqli->prepare($query);
	    $stmt->execute();
	    $result = $stmt->get_result();	
		
	    print_result($result);
		
}

function print_result($result) {

		     header("Content-Type: text/json");
		     $return_arr =array();

         while ($row=$result->fetch_assoc()) {
                $vehicleInstance = new Vehicle($row['vehicleId'], $row['currentTime'], $row['status'], $row['position'], $row['battery'], $row['distanceTravelled'], $row['destination'], $row['freeSeats'], $row['speed']);
                $row_array = $vehicleInstance->jsonSerialize();
				
                array_push($return_arr,$row_array);	    
        } 
         echo json_encode($return_arr);
  }

?>